package carassius.GUI;

import carassius.DAL.AnimalRow;
import carassius.DAL.AquariumRow;
import carassius.DAL.Database;
import carassius.DAL.OrganismListRow;
import java.awt.Frame;
import java.awt.Window;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author siebz0r
 */
public class AnimalListTable
		extends TableWithMenu
{
	private AquariumRow _selectedAquarium;
	private Window _parentWindow;

	public AnimalListTable()
	{
		super();
		setTableHeader(null);
		DefaultTableModel model = new DefaultTableModel();
		model.addColumn("organismListColumn");
		setModel(model);
	}

	public AnimalListTable(Frame parent)
	{
		super();
		_parentWindow = parent;
		setTableHeader(null);
		DefaultTableModel model = new DefaultTableModel(0, 0);
		model.addColumn("organismListColumn");
		setModel(model);
		TableColumn organismListColumn = getColumnModel().getColumn(0);
		organismListColumn.setCellRenderer(new AnimalTableCellProcessor(_parentWindow));
		organismListColumn.setCellEditor(new AnimalTableCellProcessor(_parentWindow));
	}

	public void setSelectedAquarium(AquariumRow aquarium)
	{
		_selectedAquarium = aquarium;
	}

	@Override
	void add()
	{
		if (_selectedAquarium != null)
		{
			AddAnimalToAquariumForm form = new AddAnimalToAquariumForm(_parentWindow, _selectedAquarium);
			form.setVisible(true);
		}
	}

	@Override
	void remove()
	{
		if (getSelectedRow() != -1
				&& getValueAt(getSelectedRow(), 0) instanceof OrganismListRow)
		{
			getCellEditor(getSelectedRow(), 0).cancelCellEditing();
			OrganismListRow row = (OrganismListRow) getValueAt(getSelectedRow(), 0);
			if (row.getOrganismId() >= 0
					&& JOptionPane.showConfirmDialog(_parentWindow,
					ResourceBundle.getBundle("carassius.resources.Messages").getString("RemoveAnimalFromAquarium"),
					ResourceBundle.getBundle("carassius.resources.Messages").getString("RemoveAnimalFromAquariumTitle"),
					JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION)
			{

				try
				{
					Database.getOrganismListTable().delete(row);
				}
				catch (SQLException ex)
				{
					JOptionPane.showMessageDialog(_parentWindow,
							ResourceBundle.getBundle("carassius.resources.Messages").getString("RemoveAnimalFromAquariumError"),
							ResourceBundle.getBundle("carassius.resources.Messages").getString("RemoveAnimalFromAquariumErrorTitle"),
							JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}

	@Override
	void edit()
	{
		if (getSelectedRow() != -1
				&& getValueAt(getSelectedRow(), 0) instanceof OrganismListRow)
		{
			OrganismListRow row = (OrganismListRow) getValueAt(getSelectedRow(), 0);
			if (row.getOrganismId() >= 0)
			{
				try
				{
					AnimalForm af = new AnimalForm(_parentWindow, (AnimalRow) Database.getAnimalTable().getByPrimaryKey(row.getOrganismId()));
					af.setVisible(true);
					getCellEditor(getSelectedRow(), 0).stopCellEditing();
				}
				catch (SQLException ex)
				{
					System.out.println(ex.toString());
				}
			}
		}
	}
}
